package com.example.administrator.about_huanxin.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.about_huanxin.R;
import com.example.administrator.about_huanxin.adapter.SearchAdapter;
import com.example.administrator.about_huanxin.model.UserEntity;
import com.example.administrator.about_huanxin.presenter.activity.SearchActivityPresener;
import com.example.administrator.about_huanxin.presenter.activity.SearchActivityPresenerImp;
import com.example.administrator.about_huanxin.view.activity.SearchActivityView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity implements SearchActivityView, SearchAdapter.OnSelectUserListener {

    @Bind(R.id.search_ed)
    EditText searchEd;
    @Bind(R.id.search_btn)
    Button searchBtn;
    @Bind(R.id.search_recy)
    RecyclerView searchRecy;

    private SearchActivityPresener searchActivityPresener;
    private SearchAdapter adapter;

    private Handler h = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    showToast("添加好友成功");
                    break;

                case 1:
                    showToast("添加好友失败");
                    break;
            }
        }
    };

    private void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setTitleText(TextView tv) {
        tv.setText("搜索用户");
    }

    @Override
    public void setVisibleBtn(Button btn) {
        btn.setText("后退");
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public View addContent(LayoutInflater inflater, FrameLayout content) {
        View view = inflater.inflate(R.layout.activity_search, content, true);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initContent(View view) {
        searchRecy.setLayoutManager(new LinearLayoutManager(this));
        searchActivityPresener = new SearchActivityPresenerImp(this);
        adapter = new SearchAdapter(this, null);
        searchRecy.setAdapter(adapter);

        adapter.setOnSelectUserListener(this);
    }

    @OnClick(R.id.search_btn)
    public void onClick() {
        if (!TextUtils.isEmpty(searchEd.getText().toString())) {
            searchActivityPresener.searchFriend(searchEd.getText().toString());
        }
    }

    @Override
    public void getSearchResult(boolean bool, List<UserEntity> lists) {
        if (bool) {
            Log.e("okok", lists.get(0).getNickname() + "..." + lists.get(0).getIcon().getUrl());
            adapter.setmDatas(lists);
        } else {

        }
    }

    @Override
    public void getAddResult(boolean bool, int tag) {
        Message msg = Message.obtain();
        if (bool) {
            msg.what = 0;
        } else {
            msg.what = 1;
        }
        h.sendMessage(msg);
    }

    @Override
    public void onSelect(UserEntity userEntity) {
        searchActivityPresener.addFriendAction(userEntity.getUsername());
    }
}
